package de.lns.entities;

import de.lns.level.Circle;
import de.lns.level.Rectangle;
import de.lns.level.Shapes;
import de.lns.tick.UpdateThread;
/**
 * 
 * @author Jascha, Frederick and Vincent
 *
 */
public class Hitbox {
	/**
	 * 
	 * @param s1 The first shape
	 * @param s2 The second shape
	 * @return True if the shapes overlap; False if they don't
	 */
	public static boolean checkhit(Shapes s1, Shapes s2){
		if(s1 == null || s2 == null){
			UpdateThread.kill("Null pointer in Hitbox check", new NullPointerException());
		}
		if(s1 instanceof Circle){
			Circle c1 = (Circle)s1;
			if(s2 instanceof Circle){
				Circle c2 = (Circle)s2;
				double touch = c1.getRadius() + c2.getRadius();
				if(touch>Math.hypot(c1.getCenterX()-c2.getCenterX(), c1.getCenterY()-c2.getCenterY())){
					return true;
				}else{
					return false;
				}
			}else{
				Rectangle r2 = (Rectangle)s2;
				if(c1.getCenterX() + c1.getRadius() < r2.getCornerX()){
					return false;
				}else if(c1.getCenterX() - c1.getRadius() > r2.getCornerX()+r2.getWidth()){
					return false;
				}else if(c1.getCenterY() + c1.getRadius() < r2.getCornerY()){
					return false;
				}else if(c1.getCenterY() - c1.getRadius() > r2.getCornerY()+r2.getHeight()){
					return false;
				}else{
					return true;
				}
			}
		} else{
			Rectangle r1 = (Rectangle)s1;
			if(s2 instanceof Circle){
				Circle c2 = (Circle)s2;
				if(c2.getCenterX() + c2.getRadius() < r1.getCornerX()){
					return false;
				}else if(c2.getCenterX() - c2.getRadius() > r1.getCornerX()+r1.getWidth()){
					return false;
				}else if(c2.getCenterY() + c2.getRadius() < r1.getCornerY()){
					return false;
				}else if(c2.getCenterY() - c2.getRadius() > r1.getCornerY()+r1.getHeight()){
					return false;
				}else{
					return true;
				}
			}else{
				Rectangle r2 = (Rectangle)s2;
				if(r1.getCornerX()>r2.getCornerX()+r2.getWidth() ||
						r1.getCornerX()+r1.getWidth()<r2.getCornerX() ||
						r1.getCornerY()>r2.getCornerY()+r2.getHeight() ||
						r1.getCornerY()+r1.getHeight()<r2.getCornerY()){
					return false;
				}else{
					return true;
				}
			}
		}
	}
}
